home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swaga_c.zip / CMDLINE.SWG / 0016_Process Commandline flags.pas < prev   
Pascal/Delphi Source File  |  1995-02-28  |  8KB  |  276 lines

  1.  
  2. (*
  3.   This unit will process command line flags, (/N -N)
  4. a) as present or absent (Is_Param)
  5. b) with an integer (eg. /N54 /X-76) (Param_Int)
  6. c) with a real number (eg /J-987.65) (Param_Real)
  7. d) with strings, including delimited strings with embedded spaces
  8.   ( eg. /X"This is the story!" /YFred)
  9.  
  10.  Routines are included to count and return the parameters that
  11.  aren't flags (Non_Flag_Count), and to return them without
  12.  counting the flag parameters (Non_Flag_Param).
  13.  
  14.  So ( /X76 Filename.txt /N"My name is Fred." George ) would count
  15.  two non-flag params, #1 = filename.txt and #2 = george.
  16.  
  17.  This is completely public domain, all I want in return for your use
  18.  is appreciation. If you improve this unit, please let me know.
  19.  Some possible improvements would be to allow embedded strings in
  20.  non-flag parameters. I haven't done this because I haven't needed
  21.  it.
  22.  
  23.  
  24.  Jim Walsh CIS:72571,173
  25.  
  26. *)
  27.  
  28. {$D-} { debugging information off }
  29. {$X+} { allow extended syntax }
  30. {$V+} { require same var type }
  31. {$R-} { range checking off }
  32. {$E+} { Add 8087 software emulation code }
  33. {$N+} { Use the 8087 software emulation code }
  34. {$I-} { Enable IOResult for I/O checking }
  35. {$B-} { Short-cut boolean evauation }
  36. {$O+} { Allow this to be a part of an overlay }
  37.  
  38. UNIT CmdLiner;
  39.  
  40.  
  41. INTERFACE
  42.  
  43.  function Is_Param(flag:Char) : Boolean;
  44.  { Responds yes if the flag (ie N) is found in the command line (ie /N or -N) }
  45.  
  46.  function Param_Int(flag:Char) : LongInt;
  47.  { Returns the integer value after the parameter, ie -M100, or -M-123 }
  48.  
  49.  function Param_Real(flag:Char) : Real;
  50.  { Returns the Real value after the parameter, ie -X654.87, or -x-3.14159 }
  51.  
  52.  function Param_Text(flag:Char) : String;
  53.  { Returns the string after the parameter, ie -MHello -> 'Hello',}
  54.  { -m"This is it, baby" -> 'This is it, baby', valid string delims='' "" [] }
  55.  
  56.  function Non_Flag_Param(index:integer) : string;
  57.  { Returns the indexth parameter, not preceded with a flag delimeter }
  58.  { /X Text.txt /Y876.76 /G"Yes sir!" MeisterBrau /? }
  59.  { For this command line 'Text.txt' is Non Flag Param #1,}
  60.  {and 'MeisterBrau is #2.}
  61.  { NB: Delimeted Non flag parameters (eg "Meister Brau") }
  62.  { not currently supported. }
  63.  
  64.  function Non_Flag_Count : integer;
  65.  { Returns the number of non-flag type parameters }
  66.  
  67.  
  68. IMPLEMENTATION
  69. const
  70.  flag_delims  : Set of Char = ['/','-'];
  71.  no_of_string_delims = 3;
  72. type
  73.  string_delim_type = Array[1..3] of record
  74.   start, stop : char
  75.  end;
  76. const
  77.  string_delims : string_delim_type = ((start:#39; stop:#39),
  78.   (start:#34; stop:#34),
  79.   (start:'['; stop:']'));
  80.  
  81.  
  82. function LowerCaseChar(c:char):char;
  83. begin
  84.  if (c>='A') and (c<='Z') Then LowerCaseChar:=Char(Ord(c)+$20)
  85.   Else LowerCaseChar:=c;
  86. end;
  87.  
  88.  
  89. {----------------------------------------------------------------------------}
  90.  function WhereFlagOccurs(flag:Char) : integer;
  91.  { returns the index number of the paramter where the flag occurs }
  92.  { if the flag is never found, it returns 0}
  93.  var
  94. ti1 : integer;
  95. finished : boolean;
  96. paramcnt : integer;
  97. ts1 : string;
  98.  begin
  99. flag:=LowerCaseChar(flag);
  100. finished:=false;
  101. ti1:=1;
  102. paramcnt:=ParamCount;
  103. While Not(finished) Do begin
  104.  If ti1>paramcnt Then begin
  105. finished:=true;
  106. ti1:=0;
  107.  end Else begin
  108. ts1:=ParamStr(ti1);
  109. If (ts1[1] In flag_delims) AND (LowerCaseChar(ts1[2])=flag) Then finished:=true;
  110.  end;
  111.  If Not(finished) Then Inc(ti1);
  112. end; {While}
  113. WhereFlagOccurs:=ti1;
  114.  end;
  115.  
  116. {----------------------------------------------------------------------------}
  117.  function Is_Param(flag:Char) : Boolean;
  118.  begin
  119. If WhereFlagOccurs(flag)=0 Then Is_Param:=false Else Is_Param:=true;
  120.  end;
  121.  
  122. {----------------------------------------------------------------------------}
  123.  function Param_Int(flag:Char) : LongInt;
  124.  var
  125. param_loc : integer;
  126. result: longint;
  127. ts1  : string;
  128. ti1  : integer;
  129.  begin
  130. param_loc:=WhereFlagOccurs(flag);
  131. If param_loc=0 Then result:=0
  132. Else begin
  133.  ts1:=ParamStr(param_loc); { Get the string }
  134.  ts1:=Copy(ts1,3,255); { Get rid of the delim and the flag }
  135.  Val(ts1,result,ti1); { Make the value }
  136.  If ti1<>0 Then result:=0; { Make sure there is no error }
  137. end; {If/Else}
  138. Param_Int:=result
  139.  end;
  140.  
  141. {----------------------------------------------------------------------------}
  142.  function Param_Real(flag:Char) : Real;
  143.  var
  144. param_loc : integer;
  145. result: real;
  146. ts1  : string;
  147. ti1  : integer;
  148.  begin
  149. param_loc:=WhereFlagOccurs(flag);
  150. If param_loc=0 Then result:=0.0
  151. Else begin
  152.  ts1:=ParamStr(param_loc); { Get the string }
  153.  ts1:=Copy(ts1,3,255); { Get rid of the delim and the flag }
  154.  Val(ts1,result,ti1); { Make the value }
  155.  If ti1<>0 Then result:=0.0;  { Make sure there is no error }
  156. end; {If/Else}
  157. Param_Real:=result;
  158.  end;
  159.  
  160. {----------------------------------------------------------------------}
  161.  function Which_String_Delim(S:string) : byte;
  162.  { Returns the index of the strings first character in the array
  163. of string_delims, if the first char of S isn't a delim it returns 0 }
  164.  var
  165. tc1 : char;
  166. tb1 : byte;
  167. finished : boolean;
  168. result  : byte;
  169.  begin
  170. tc1:=S[1];
  171. tb1:=1;
  172. finished:=false;
  173. While Not(finished) Do begin
  174.  If tb1>no_of_string_delims Then begin
  175. result:=0;
  176. finished:=true;
  177.  end Else begin
  178. If tc1=string_delims[tb1].start Then begin
  179.  result:=tb1;
  180.  finished:=true;
  181. end;
  182.  end;
  183.  If Not(finished) Then Inc(tb1);
  184. end; {While}
  185. Which_String_Delim:=result;
  186.  end; {function Which_String}
  187.  
  188. {-------------------------------------------------------------------------}
  189.  function Param_Text(flag:Char) : String;
  190.  var
  191. param_loc : integer;
  192. param_cnt : integer;
  193. result: string;
  194. ts1  : string;
  195. ti1  : integer;
  196. s_delim  : byte; { This should be 0(no string), 1', 2", 3[ }
  197. finished : boolean;
  198.  begin
  199. param_loc:=WhereFlagOccurs(flag);
  200. If param_loc=0 Then result:=''
  201. Else begin
  202.  ts1:=ParamStr(param_loc); { Get the string }
  203.  ts1:=Copy(ts1,3,255); { Get rid of the delim and the flag }
  204.  { See if the first char of ts1 is one of the string_delims }
  205.  s_delim:=Which_String_Delim(ts1);
  206.  If s_delim=0 Then result:=ts1
  207.  Else begin
  208. result:=Copy(ts1,2,255);{ Drop the s_delim }
  209. finished:=false;
  210. param_cnt:=ParamCount;
  211. While Not(finished) Do begin
  212.  Inc(param_loc);
  213.  If param_loc>param_cnt Then finished:=true
  214.  Else begin
  215. ts1:=ParamStr(param_loc);
  216. If ts1[Length(ts1)]=string_delims[s_delim].stop Then finished:=true;
  217. result:=result+' '+ts1;
  218.  end; { If/Else }
  219. end; { While }
  220. result[0]:=Char(Length(result)-1); { Drop the last delimeter }
  221.  end; { If/Else a delimited string }
  222. end; { If/Else the flag is found }
  223. Param_Text:=result;
  224.  end;
  225.  
  226. {---------------------------------------------------------------------------}
  227.  function Non_Flag_Param(index:integer) : string;
  228.  var
  229. param_cnt : integer;
  230. ti1  : integer;
  231. ts1  : string;
  232. finished : boolean;
  233. cur_index : integer;
  234.  begin
  235. param_cnt:=ParamCount;
  236. cur_index:=0;
  237. ti1:=0;
  238. finished:=false;
  239. While Not(finished) Do begin
  240.  Inc(ti1);
  241.  IF cur_index>param_cnt Then begin
  242. ts1:='';
  243. finished:=true;
  244.  end Else begin
  245. ts1:=ParamStr(ti1);
  246. If Not(ts1[1] IN flag_delims) Then begin
  247.  Inc(cur_index);
  248.  If cur_index=index Then finished:=true;
  249. end;
  250.  end; {If/Else}
  251. end; {While}
  252. Non_Flag_Param:=ts1;
  253.  end;
  254.  
  255. {---------------------------------------------------------------------------}
  256.  function Non_Flag_Count : integer;
  257.  var
  258. param_cnt : integer;
  259. result: integer;
  260. ti1  : integer;
  261. ts1  : string;
  262.  begin
  263. param_cnt:=ParamCount;
  264. result:=0;
  265. ti1:=0;
  266. For ti1:=1 To param_cnt Do begin
  267.  ts1:=ParamStr(ti1);
  268.  If Not(ts1[1] IN flag_delims) Then begin
  269. Inc(result);
  270.  end;
  271. end; {For}
  272. Non_Flag_Count:=result;
  273.  end;
  274.  
  275. END.
  276.